package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.DepMapper;
import cn.itcast.erp.entity.Dep;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.DepService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 部门Service类
 * @author 崔凯铭
 * @date 2019/6/5
 */

@Service("depService")
public class DepServiceImpl implements DepService {

    @Resource
    private DepMapper depMapper;

    @Override
    public int insertDep(Dep dep) {
        if (dep != null){
            int i = this.depMapper.insertDep(dep);
            return i;
        }
        return -1;
    }

    @Override
    public int deleteDep(Long uuid) {
        if (uuid != null){
            int i = this.depMapper.deleteDep(uuid);
            return i;
        }
        return -1;
    }

    @Override
    public int updateDep(Dep dep) {
        if (dep != null){
            int i = this.depMapper.updateDep(dep);
            return i;
        }
        return -1;
    }


    @Override
    public AjaxResult findDep(Dep dep) {
        int total = this.depMapper.count(dep);
        List<Dep> rows = this.depMapper.findDep(dep);
        AjaxResult ar = new AjaxResult();
        ar.setTotal(total);
        ar.setRows(rows);
        return ar;

    }

    @Override
    public List<Dep> findAllDep(Dep dep) {
      return depMapper.findAllDep (dep);
    }

    @Override
    public Dep findDepById(Long uuid) {
        return this.depMapper.findDepById (uuid);
    }


    //导出Excel
    @Override
    public InputStream getAllDepEx() {
        //添加数据
        List<Dep> dep = depMapper.getAllDepEx();

        HSSFWorkbook sheets = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = sheets.createSheet("部门信息表");  // 创建第一个新的Sheet页


        //创建第一行（也可以成为表头）
        Row row1 = sheet.createRow(0);
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 30 * 256);
        sheet.setColumnWidth(2, 30 * 256);

        //给表头第一行一次创建单元格
        String[] headers = {"编号","部门名称","联系电话"};
        for(int i = 0; i < headers.length; i++){
            row1.createCell(i).setCellValue(headers[i]);
            row1.setHeightInPoints(30);//设置行的高度是30
        }


        //向单元格里添加数据
        for(short i=0;i<dep.size();i++){
            row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(dep.get(i).getUuid());
            row1.createCell(1).setCellValue(dep.get(i).getName());
            row1.createCell(2).setCellValue(dep.get(i).getTele());
            row1.setHeightInPoints(30);//设置行的高度是30
        }
        //字节流缓存区
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            sheets.write(bo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ByteArrayInputStream(bo.toByteArray());
    }

}
